package Config;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WarehouseConfig {
    private int cellSize;
    private int gridColumns;
    private int gridRows;
    private String URL;
    private String username;
    private String password;
    private List<int[]> inboundPositions = new ArrayList<>();
    private List<int[]> outboundPositions = new ArrayList<>();
    private List<int[]> chargingStationsPositions = new ArrayList<>();

    public void loadConfig(String filePath) {
        Map<String, String> config = new HashMap<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                line = line.trim();
                if (!line.isEmpty() && !line.startsWith("#") && line.contains("=")) {
                    String[] parts = line.split("=", 2);
                    if (parts.length == 2) {
                        config.put(parts[0].trim(), parts[1].trim());
                    }
                }
            }
        } catch (IOException e) {
            System.err.println("Error reading config file: " + e.getMessage());
        }

        // 解析基础配置
        cellSize = Integer.parseInt(config.getOrDefault("cellSize", "30"));
        gridColumns = Integer.parseInt(config.getOrDefault("gridColumns", "30"));
        gridRows = Integer.parseInt(config.getOrDefault("gridRows", "20"));

        // 解析坐标列表
        inboundPositions = parsePositions(config.getOrDefault("inboundPositions", ""));
        outboundPositions = parsePositions(config.getOrDefault("outboundPositions", ""));
        chargingStationsPositions = parsePositions(config.getOrDefault("chargingStationsPositions", ""));

        // 解析数据库配置
        URL = config.getOrDefault("URL", "jdbc:mysql://localhost:3306/roboticwarehouse");
        username = config.getOrDefault("username", "root");
        password = config.getOrDefault("password", "");
    }

    private List<int[]> parsePositions(String positionsString) {
        List<int[]> positions = new ArrayList<>();
        if (!positionsString.isEmpty()) {
            String[] pairs = positionsString.split(";");
            for (String pair : pairs) {
                String[] coords = pair.split(",");
                if (coords.length == 2) {
                    try {
                        int x = Integer.parseInt(coords[0].trim());
                        int y = Integer.parseInt(coords[1].trim());
                        positions.add(new int[]{x, y});
                    } catch (NumberFormatException e) {
                        System.err.println("Invalid coordinate: " + pair);
                    }
                }
            }
        }
        return positions;
    }

    @Override
    public String toString() {
        return "WarehouseConfig{" +
                "cellSize=" + cellSize +
                ", gridColumns=" + gridColumns +
                ", gridRows=" + gridRows +
                ", inboundPositions=" + formatPositions(inboundPositions) +
                ", outboundPositions=" + formatPositions(outboundPositions) +
                ", chargingStationsPositions=" + formatPositions(chargingStationsPositions) +
                '}';
    }

    private String formatPositions(List<int[]> positions) {
        StringBuilder sb = new StringBuilder();
        for (int[] pos : positions) {
            sb.append("(").append(pos[0]).append(", ").append(pos[1]).append(") ");
        }
        return sb.toString().trim();
    }

    public int getCellSize() {
        return cellSize;
    }

    public int getGridColumns() {
        return gridColumns;
    }

    public int getGridRows() {
        return gridRows;
    }

    public List<int[]> getInboundPositions() {
        return inboundPositions;
    }

    public List<int[]> getOutboundPositions() {
        return outboundPositions;
    }

    public List<int[]> getChargingStationsPositions() {
        return chargingStationsPositions;
    }

    public String getURL() {
        return URL;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public static void main(String[] args) {
        WarehouseConfig config = new WarehouseConfig();
        config.loadConfig("./src/Config/config.txt");
        System.out.println(config);
        //System.out.println(config.inboundPositions);
    }
}

